package com.daykw.wenshu.task;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.wenshu.config.NutzDaoConfig;
import com.daykw.wenshu.model.CaseText;
import com.daykw.wenshu.model.CourtUrl;
import com.daykw.wenshu.utils.SpringContextHolder;

public class PaserArticleJob extends Thread {

	private static Logger logger = Logger.getLogger(PaserArticleJob.class);

	@Override
	public void run() {

		try {
			NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

			// List<CourtUrl> ds = dao.query(CourtUrl.class, Cnd.wrap("src = '0' LIMIT
			// 1000"));
			//
			// for (CourtUrl task : ds) {
			// this.work(task.getDoc_id());
			// }

			List<Record> ds = dao.query("t_wenshu_havetag", Cnd.wrap("status =0  LIMIT 1000"));

			for (Record task : ds) {
				String docid = task.getString("doc_id");
				//CaseText txt = dao.fetch(CaseText.class, Cnd.where("doc_id", "=", docid));
				
				this.work(docid);
				dao.update("t_wenshu_havetag", Chain.make("status", 2), Cnd.where("doc_id", "=", docid));
			}

			logger.info("处理完毕" + ds.size());
		} catch (Exception e) {
			logger.error("", e);
		}

	}

	private void work(String docid) {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);

		CaseText caset = dao.fetch(CaseText.class, Cnd.where("doc_id", "=", docid));

		String html = caset.getHtml();
		String content = "";

		if (StringUtils.isEmpty(html)) {
			logger.info("html为空:" + docid);
			dao.update(CourtUrl.class, Chain.make("src", '3'), Cnd.where("doc_id", "=", docid));
			return;
		}
		content = html.replace("\\", "");
		String cc = "";
		if (content.indexOf("<pstyle") >= 0 || content.indexOf("<spanstyle") >= 0 || content.indexOf("<pclass") >= 0
				|| content.indexOf("<palign") >= 0 || content.indexOf("<spanlang") >= 0) {
			content = content.replace("<pstyle", "<p style");
			content = content.replace("<spanstyle", "<span style");
			content = content.replace("<pclass", "<p class");
			content = content.replace("<palign", "<p align");
			content = content.replace("<spanlang", "<span lang");

			content = content.replace("”", "'");
			Document doc = Jsoup.parse(content);

			List<Element> tn = doc.getElementsByTag("p");
			for (Element tt : tn) {

				cc = cc + tt.text() + "_|_";
			}
			cc = cc.replace("\r\n", "_|_");
		} else {
			content = content.replace("”", "'");
			Document doc = Jsoup.parse(content);

			List<Element> tn = doc.getElementsByTag("div");
			for (Element tt : tn) {

				cc = cc + tt.text() + "_|_";
			}
			cc = cc.replace("\r\n", "_|_");

		}
		cc = cc.replace("p t ” >", "");
		
		int txt_len = cc.length();
		dao.update(CaseText.class, Chain.make("txt", cc).add("txt_len", txt_len), Cnd.where("doc_id", "=", docid));

		dao.update(CourtUrl.class, Chain.make("src", '2'), Cnd.where("doc_id", "=", docid));
		logger.info("解析完毕:" + docid);

	}

}
